//
//  MMGVenderLoginManager.m
//  MyApp
//
//  Created by Don on 2017/9/6.
//  Copyright © 2017年 com.memego. All rights reserved.
//

#import "MMGVenderSocialManager.h"
//#import <TencentOpenAPI/TencentOAuth.h>
#import "NSDictionary+JsonString2Dic.h"

@implementation MMGVenderSocialManager
SYNTHESIZE_SINGLETON_FOR_CLASS(MMGVenderSocialManager);

- (void)signInWithVenderPlatform:(UMSocialPlatformType)platformType inViewController:(UIViewController *)viewController completion:(void (^ _Nullable)(NSString *code,NSString *userInfoJsonString))completion {
    
    if (platformType == UMSocialPlatformType_QQ || platformType == UMSocialPlatformType_WechatSession) {
//        PWLoginSourceType sourceType = PWLoginSourceTypeQQ;
        NSString *platformName = @"QQ";
        if (platformType == UMSocialPlatformType_WechatSession) {
            platformName = @"微信";
//            sourceType = PWLoginSourceTypeWechat;
        }
        
        [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:viewController completion:^(id result, NSError *error) {
            /*NSDictionary *params = @{@"errCode":@"40001",
                                     @"errMessage":@"",
                                     @"data":@""
                                    };
             */
            NSString *erroCode = nil;
            //NSString *errMsg = nil;
            NSString *data = nil;
            if (!error && [result isKindOfClass:[UMSocialUserInfoResponse class]]) {
                UMSocialUserInfoResponse *userinfo = result;
                erroCode = @"200";
                //errMsg = @"";
                data = [NSDictionary mmg_convertToStringViaJSONData:@{
                  @"openid":userinfo.openid.length > 0 ? userinfo.openid : @"",@"unionid":userinfo.uid.length > 0 ? userinfo.uid : @"",@"nickname":userinfo.name.length > 0 ? userinfo.name : @"",@"headimgurl":userinfo.iconurl.length > 0 ? userinfo.iconurl : @""}];

            
            } else if (error && error.code == 2008) {
                
                erroCode = [NSString stringWithFormat:@"%d",error.code];
                data = [NSString stringWithFormat:@"用户未安装%@", platformName];
                
            } else if (error && error.code == 2009) {
                erroCode = [NSString stringWithFormat:@"%d",error.code];
                data = [NSString stringWithFormat:@"%@登录取消", platformName];
            } else {
                erroCode = [NSString stringWithFormat:@"%d",error.code];
                data = [NSString stringWithFormat:@"%@登录出错，请重试", platformName];
            }
            
           /* params = @{@"errCode":(erroCode != nil && erroCode.length>0) ? erroCode : @"",
                       @"errMessage":(errMsg != nil && errMsg.length>0) ? errMsg : @"",
                       @"data":data != nil ? data : @""
                       };
            */
            
//            [SVProgressHUD showInfoWithStatus:errMsg];
            if (completion) {
                completion(erroCode,data);
            }
        }];
    }
}
@end
